/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.inventory.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtAccessoryInout;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtAccessoryStore;
import com.thinkgem.jeesite.modules.inventory.service.ZlmtAccessoryInoutService;
import com.thinkgem.jeesite.modules.inventory.service.ZlmtAccessoryStoreService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 备件Controller
 * @author johnKee
 * @version 2016-07-28
 */
@Controller
@RequestMapping(value = "${adminPath}/inventory/zlmtAccessoryStore")
public class ZlmtAccessoryStoreController extends BaseController {

	@Autowired
	private ZlmtAccessoryStoreService zlmtAccessoryStoreService;

	@Autowired
	private ZlmtAccessoryInoutService zlmtAccessoryInoutService;
	
	@ModelAttribute
	public ZlmtAccessoryStore get(@RequestParam(required=false) String id) {
		ZlmtAccessoryStore entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = zlmtAccessoryStoreService.get(id);
		}
		if (entity == null){
			entity = new ZlmtAccessoryStore();
		}
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(ZlmtAccessoryStore zlmtAccessoryStore, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ZlmtAccessoryStore> page = zlmtAccessoryStoreService.findPage(new Page<ZlmtAccessoryStore>(request, response), zlmtAccessoryStore); 
		model.addAttribute("page", page);
		return "modules/inventory/zlmtAccessoryStoreList";
	}

	@RequestMapping(value = "form")
	public String form(ZlmtAccessoryStore zlmtAccessoryStore, Model model) {
		model.addAttribute("zlmtAccessoryStore", zlmtAccessoryStore);
		return "modules/inventory/zlmtAccessoryStoreForm";
	}

	@RequestMapping(value = "save")
	public String save(ZlmtAccessoryStore zlmtAccessoryStore, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, zlmtAccessoryStore)){
			return form(zlmtAccessoryStore, model);
		}
		boolean shouldInOut = false;
		if(StringUtils.isNotEmpty(zlmtAccessoryStore.getId())){
			shouldInOut = false;
			ZlmtAccessoryStore oldStore = zlmtAccessoryStoreService.get(zlmtAccessoryStore.getId());
			zlmtAccessoryStore.setAmount(oldStore.getAmount());
		}else {
			shouldInOut = true;
		}
		if(shouldInOut){
			ZlmtAccessoryInout inout = new ZlmtAccessoryInout();
			inout.setAccessoryStoreId(zlmtAccessoryStore.getId());
			inout.setAmount(zlmtAccessoryStore.getAmount());
			inout.setContact(zlmtAccessoryStore.getContact());
			inout.setInoutType("IN");
			inout.setUser(UserUtils.getUser());
            zlmtAccessoryStoreService.groupSave(zlmtAccessoryStore, inout, null);
			zlmtAccessoryInoutService.save(inout);
		}else {
            zlmtAccessoryStoreService.save(zlmtAccessoryStore);
        }
		addMessage(redirectAttributes, "保存备件成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/zlmtAccessoryStore/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(ZlmtAccessoryStore zlmtAccessoryStore, RedirectAttributes redirectAttributes) {
		zlmtAccessoryStoreService.delete(zlmtAccessoryStore);
		addMessage(redirectAttributes, "删除备件成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/zlmtAccessoryStore/?repage";
	}

}